import Vue from 'vue'
export default {
	state:{
		list:[
			{id:0,name:'张先丰',num:"18864484502",site:"河南省信阳市固始县",value:"石佛店乡",select:true},
			{id:1,name:'吕文静',num:"18865284502",site:"河南省信阳市固始县",value:"方集",select:false}
		]
	},
	getter:{
		
	},
	mutations:{
		// 添加地址
		setSite(state,res){
			if(state.list.length!==0){
				res.id=state.list[state.list.length-1].id + 1
			}else{
				res.id=0
			}
			console.log(res);
			state.list.push(JSON.parse(JSON.stringify(res)))
		},
		//全部不选中
		all(state){
			state.list.forEach(v=>{
				v.select=false
			})
		},
		// 修改
		modification(state,res){
			console.log("触发了修改",res);
			state.list.forEach((v,a,b)=>{
				if(v.id===res.id){
					Vue.set(b,a,JSON.parse(JSON.stringify(res)))
				}
			})
			// console.log(state.list);
		},
		// 删除
		deletes(state,res){
				state.list.forEach((v,a,b)=>{
					if(v.id===res.id){
						b.splice(a, 1)
					}
			})
		}
	},

	actions: { 
			setsite({commit},res){
				if(res.select){
					commit("all")
				}
				
				if(res.id===''){
					commit('setSite',res)
					return
				}
				commit('modification',res)
			}
		}
}
